package com.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.UserGoodsMapper;
import com.entity.PointGoods;
import com.entity.UserGoods;
import com.entity.ZhiyuanzheEntity;
import com.service.PointGoodsService;
import com.service.UserGoodsService;
import com.service.ZhiyuanzheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserGoodsServiceImpl extends ServiceImpl<UserGoodsMapper,UserGoods> implements UserGoodsService {

    @Autowired
    private PointGoodsService pointGoodsService;
    @Autowired
    private ZhiyuanzheService zhiyuanzheService;
    @Override
    public boolean insert(UserGoods userGoods) {
        PointGoods pointGoods = pointGoodsService.selectById(userGoods.getGoodId());
        ZhiyuanzheEntity zhiyuanzheEntity = zhiyuanzheService.selectById(userGoods.getUserId());
        int point = zhiyuanzheEntity.getPoint()-pointGoods.getPoints();
        if (point>=0){
            this.baseMapper.insert(userGoods);
            zhiyuanzheEntity.setPoint(point);
            return true;
        }else {
            return false;
        }
    }
}
